<template>
  <a-card :bordered="false" class="card-area">
    <div>
      <div class="operator">
        <a-button type="primary" @click="add">创建任务</a-button>
        <a-button @click="exportExcel">导出Excel</a-button>
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :scroll="{ x: 900 }"
               @change="handleTableChange">
        <template slot="operation" slot-scope="text, record">
          <a-icon type="setting" theme="twoTone" twoToneColor="#4a9ff5" @click="edit(record)" title="处理"></a-icon>
          &nbsp;
          <a-icon type="eye" theme="twoTone" twoToneColor="#42b983" @click="view(record)" title="查看"></a-icon>
        </template>
      </a-table>
    </div>
    <!-- 新增任务 -->
    <work-order-add
      @success="handleWorkOrderAddSuccess"
      @close="handleWorkOrderAddClose"
      :workOrderAddVisible="workOrderAddVisible">
    </work-order-add>
    <!-- 处理任务 -->
    <work-order-edit
      ref="workOrderEdit"
      @close="handleWorkOrderEditClose"
      @success="handleWorkOrderEditSuccess"
      :workOrderEditVisible="workOrderEditVisible">
    </work-order-edit>
  </a-card>
</template>

<script>
import WorkOrderAdd from './WorkOrderAdd'
import WorkOrderEdit from './WorkOrderEdit'
export default {
  name: 'WorkOrder',
  components: { WorkOrderAdd, WorkOrderEdit },
  data () {
    return {
      advanced: false,
      queryParams: {},
      filteredInfo: null,
      sortedInfo: null,
      paginationInfo: null,
      dataSource: [],
      selectedRowKeys: [],
      loading: false,
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      workOrderAddVisible: false,
      workOrderEditVisible: false
    }
  },
  computed: {
    columns () {
      let { sortedInfo, filteredInfo } = this
      sortedInfo = sortedInfo || {}
      filteredInfo = filteredInfo || {}
      return [{
        title: '类型',
        dataIndex: 'type',
        customRender: (text, row, index) => {
          switch (text) {
            case 0:
              return <a-tag color="red">火警</a-tag>
            case 1:
              return <a-tag color="gold">故障</a-tag>
            case 2:
              return <a-tag color="purple">报修</a-tag>
            case 3:
              return <a-tag color="geekblue">巡查</a-tag>
            case 4:
              return <a-tag color="cyan">保养</a-tag>
            default:
              return text
          }
        },
        filters: [
          { text: '火警', value: '0' },
          { text: '故障', value: '1' },
          { text: '报修', value: '2' },
          { text: '巡查', value: '3' },
          { text: '保养', value: '4' }
        ],
        filterMultiple: false,
        filteredValue: filteredInfo.type || null,
        onFilter: (value, record) => record.type.includes(value)
      }, {
        title: '任务描述',
        dataIndex: 'name',
        width: 100
      }, {
        title: '所属项目',
        dataIndex: 'project'
      }, {
        title: '提交时间',
        dataIndex: 'createTime',
        sorter: true,
        sortOrder: sortedInfo.columnKey === 'createTime' && sortedInfo.order
      }, {
        title: '完成时间',
        dataIndex: 'completeTime',
        sorter: true,
        sortOrder: sortedInfo.columnKey === 'completeTime' && sortedInfo.order
      }, {
        title: '当前处理人',
        dataIndex: 'curUserName'
      }, {
        title: '状态',
        dataIndex: 'status',
        customRender: (text, row, index) => {
          switch (text) {
            case 0:
              return '未指派'
            case 1:
              return '抢单中'
            case 2:
              return '待处理'
            case 3:
              return '处理中'
            case 4:
              return '待审核'
            case 5:
              return '已完成'
            default:
              return text
          }
        },
        filters: [
          { text: '未指派', value: '0' },
          { text: '抢单中', value: '1' },
          { text: '待处理', value: '2' },
          { text: '处理中', value: '3' },
          { text: '待审核', value: '4' },
          { text: '已完成', value: '5' }
        ],
        filterMultiple: false,
        filteredValue: filteredInfo.status || null,
        onFilter: (value, record) => record.status.includes(value)
      }, {
        title: '操作',
        dataIndex: 'operation',
        scopedSlots: { customRender: 'operation' }
      }]
    }
  },
  mounted () {
    this.fetch()
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
      if (!this.advanced) {
        this.queryParams.createTimeFrom = ''
        this.queryParams.createTimeTo = ''
      }
    },
    view (record) {
      this.userInfo.data = record
      this.userInfo.visiable = true
    },
    handleWorkOrderAddSuccess () {
      this.workOrderAddVisible = false
      this.$message.success('新增维保任务成功')
      this.search()
    },
    handleWorkOrderAddClose () {
      this.workOrderAddVisible = false
    },
    add () {
      this.workOrderAddVisible = true
    },
    edit (record) {
      this.$refs.workOrderEdit.setFormValues(record)
      this.workOrderEditVisible = true
    },
    handleWorkOrderEditClose () {
      this.workOrderEditVisible = false
    },
    handleWorkOrderEditSuccess () {
      this.workOrderEditVisible = false
      this.$message.success('处理维保任务成功')
      this.search()
    },
    exportExcel () {
      let {sortedInfo, filteredInfo} = this
      let sortField, sortOrder
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field
        sortOrder = sortedInfo.order
      }
      this.$export('user/excel', {
        sortField: sortField,
        sortOrder: sortOrder,
        ...this.queryParams,
        ...filteredInfo
      })
    },
    search () {
      let {sortedInfo, filteredInfo} = this
      let sortField, sortOrder
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field
        sortOrder = sortedInfo.order
      }
      this.fetch({
        sortField: sortField,
        sortOrder: sortOrder,
        ...this.queryParams,
        ...filteredInfo
      })
    },
    reset () {
      // 取消选中
      this.selectedRowKeys = []
      // 重置分页
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }
      // 重置列过滤器规则
      this.filteredInfo = null
      // 重置列排序规则
      this.sortedInfo = null
      // 重置查询参数
      this.queryParams = {}
      // 清空部门树选择
      this.$refs.deptTree.reset()
      // 清空时间选择
      if (this.advanced) {
        this.$refs.createTime.reset()
      }
      this.fetch()
    },
    handleTableChange (pagination, filters, sorter) {
      // 将这三个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination
      this.filteredInfo = filters
      this.sortedInfo = sorter

      this.userInfo.visiable = false
      this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...this.queryParams,
        ...filters
      })
    },
    fetch (params = {}) {
      // 显示loading
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$get('workorder/list', {
        ...params
      }).then((r) => {
        let data = r.data
        const pagination = { ...this.pagination }
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
        // 数据加载完毕，关闭loading
        this.loading = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import "../../../static/less/Common";
</style>
